package com.example.guessnumberplus;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;


public class EditTextEx extends androidx.appcompat.widget.AppCompatEditText{
    private EditTextEx nextEditText;
    private EditTextEx prevEditText;
    private int maxLength=1;

    public EditTextEx(Context context) {
        super(context);
    }

    public EditTextEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //设置下一个EditText
   public void setNextEditText(EditTextEx editText){
        nextEditText = editText;
   }
    //设置上一个EditText
    public void setPrevEditText(EditTextEx editText){
        prevEditText = editText;
    }
    //设置最大的长度
    public void setMaxLength(int length){
        maxLength = length;
    }
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if(text.length() == maxLength && nextEditText!=null){
            nextEditText.requestFocus();
        }
        if(text.length() == 0 && lengthBefore == 1 && prevEditText != null){
            prevEditText.setSelection(prevEditText.getText().length());
            prevEditText.requestFocus();
        }
    }

}
